package com.mycontract.suitepro.contract.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mycontract.suitepro.R
import com.mycontract.suitepro.contract.ContractEvent
import com.mycontract.suitepro.data.RevObjContractOrder
import com.mycontract.suitepro.extend.copyContractSuiteToContractSuiteClipboard
import com.mycontract.suitepro.extend.recordContractSuiteClickContractSuiteEvent
import com.mycontract.suitepro.utils.analytics.util.AnalyticsUtil

class ContractOrderAdapter(private val itemList: List<RevObjContractOrder>) :
    RecyclerView.Adapter<ContractOrderAdapter.ViewHolder>() {

    private var mContractSuiteOnContractSuiteDialogContractSuiteListContractSuiteItemContractSuiteClickContractSuiteListener: (RevObjContractOrder, Int) -> Unit =
        { listItem: RevObjContractOrder, i: Int -> }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textAdapterOrderNumberValue: TextView =
            view.findViewById(R.id.textAdapterOrderNumberValue)
        val imageAdapterOrderNumberCopy: ImageView =
            view.findViewById(R.id.imageAdapterOrderNumberCopy)
        val textAdapterPartyAValue: TextView =
            view.findViewById(R.id.textAdapterPartyAValue)
        val textAdapterPartyBValue: TextView =
            view.findViewById(R.id.textAdapterPartyBValue)
        val textAdapterSubmit: TextView =
            view.findViewById(R.id.textAdapterSubmit)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ContractOrderAdapter.ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_contract_suite_adpter_contract_signed, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: ContractOrderAdapter.ViewHolder,
        position: Int
    ) {
        val item: RevObjContractOrder = itemList[position]
        holder.textAdapterOrderNumberValue.text = item.orderId
        holder.textAdapterPartyAValue.text = item.fullName
        holder.textAdapterPartyBValue.text = item.product

        println("----->canSign:${item.canSign}")

        if (item.canSign) {
            holder.textAdapterSubmit.visibility = View.VISIBLE
        } else {
            holder.textAdapterSubmit.visibility = View.GONE
        }

        holder.imageAdapterOrderNumberCopy.setOnClickListener {
            holder.imageAdapterOrderNumberCopy.context.copyContractSuiteToContractSuiteClipboard(item.orderId)

            // 打点
            AnalyticsUtil.saveHitPointInfo(
                type = ContractEvent.ORDER_TIEM_COPY,
                remark = item.orderId.trim()
            )
        }

        holder.textAdapterSubmit.setOnClickListener {
            // 回调
            mContractSuiteOnContractSuiteDialogContractSuiteListContractSuiteItemContractSuiteClickContractSuiteListener.invoke(
                item,
                position
            )
        }
    }

    override fun getItemCount(): Int {
        return itemList.size
    }

    fun setContractSuiteOnContractSuiteItemContractSuiteClickContractSuiteListener(listener: (RevObjContractOrder, Int) -> Unit) {
        mContractSuiteOnContractSuiteDialogContractSuiteListContractSuiteItemContractSuiteClickContractSuiteListener = listener
    }
}